import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/core/controllers/auth_controller.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/base_entity.dart';
import 'package:oramemo/entity/circle_user_entity.dart';
import 'package:oramemo/entity/msg_entity.dart';
import 'package:oramemo/pages/home/home_logic.dart';

class MembersLogic extends GetxController {
  List<CircleUserDataSysUser> list = [];
  bool delPermission = true;

  @override
  void onInit() async {
    await load();
    super.onInit();
  }

  Future<void> load() async {
    final deviceId = HomeLogic.to.currentDeviceId;
    Map<String, dynamic>? res = await DefaultApi()
        .oraDeviceUserCircleGet<Map<String, dynamic>>(deviceId: deviceId);
    list.clear();
    if (res?['data']?.isNotEmpty == true) {
      final userId = AuthController.to.user?.userId;
      for (Map<String, dynamic> data in res!['data']) {
        if (data['sysUser'] != null) {
          CircleUserDataSysUser user =
              CircleUserDataSysUser.fromJson(data['sysUser']);
          list.add(user);
          if (user.userId == userId) {
            // delPermission = data['devicePermission'] == 1;
          }
        }
      }
    }
    update();
  }

  void delete(int index) async{
    CircleUserDataSysUser user = list[index];
    BaseEntity? res = await DefaultApi()
        .oraDeviceUserCircleIdDelete<BaseEntity>(id: user.userId?.toString()??'',isShowLoading: true);
    if(res?.isSuccess==true) {
      Toast.show('删除成功');
      list.remove(user);
    }
    update();
  }
}
